package com.sinai.mshab.client.ui.filter;

import java.util.HashMap;
import java.util.Iterator;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.sinai.mshab.client.ui.Key;
import com.sinai.mshab.client.ui.aggregate.FormData;
import com.sinai.mshab.client.ui.aggregate.HorizontalComponentList;
import com.sinai.mshab.client.ui.aggregate.TableComponent;

/**
 * This is a full filtered table, allowing adding of filter aggregates and
 * filters, allowing sorting by criteria, and showing certain number of records
 * per page, as well as page navigation
 * 
 * @author serge
 * 
 */
public class AdvancedFilteredTable extends FilteredTable {
	
	public AdvancedFilteredTable() {
		
		fieldsToCustomFilters = new HashMap();
		
		table = new TableComponent(true, historyChangePrefix);
		table.setStyleName("sortableTable");

		// go through the keys and add columns.
		Key key = FormData.getKey();
		String table_name = key.getTableName();
		Iterator it = key.getKeys().keySet().iterator();

		// walking through the cols of the key and adding them as cols to the
		// table.
		while (it.hasNext()) {
			String col_name = (String) it.next();
			column_names.add(table_name + "." + col_name);
		}

		// another FilterAggregate button
		Button add_FilterAggregate_button = null;
		add_FilterAggregate_button = new Button("Add a Category");
		add_FilterAggregate_button.setStyleName("button");
		add_FilterAggregate_button
				.addClickListener(new AddFilterAggregateListener());

		// populate button
		Button populate_button = new Button("Run Report");
		populate_button.setStyleName("button");
		populate_button.addClickListener(new UpdateListener());

		// the hpanel for buttons.
		HorizontalComponentList button_panel = null;
		button_panel = new HorizontalComponentList();
		button_panel.add(add_FilterAggregate_button);
		button_panel.add(populate_button);

		// creating the sort by field.
		Label sortBylabel = new Label("Sort By:");
		sortBylabel.setStyleName("sub_description_label");
		sort_by = new ListBox();
		sort_by.addFocusListener(this);

		// creating the sort order field.
		Label sortOrderlabel = new Label("Sort Order:");
		sortOrderlabel.setStyleName("sub_description_label");
		sort_asc_desc = new ListBox();
		sort_asc_desc.addItem("Ascending");
		sort_asc_desc.addItem("Descending");
		sort_asc_desc.addFocusListener(this);

		// creating records per page dropdown
		Label recordsPerPagelabel = new Label("Show Records:");
		recordsPerPagelabel.setStyleName("sub_description_label");
		records_per_page = new ListBox();
		records_per_page.addItem("2");
		records_per_page.addItem("5");
		records_per_page.addItem("10");
		records_per_page.addItem("25");
		records_per_page.addItem("50");
		records_per_page.addItem("100");
		records_per_page.setSelectedIndex(4);
		records_per_page.addFocusListener(this);

		// the hpanel for sort by
		HorizontalComponentList sortby_panel = new HorizontalComponentList();
		sortby_panel.add(sortBylabel);
		sortby_panel.add(sort_by);
		sortby_panel.add(sortOrderlabel);
		sortby_panel.add(sort_asc_desc);
		sortby_panel.add(recordsPerPagelabel);
		sortby_panel.add(records_per_page);
		sortby_panel.setStyleName("lightshadebox");

		// the showing records label, dont need it for now
		showing_records = new Label();
		showing_records.setStyleName("important_blue");

		// nav panel for records
		record_navigation = new HorizontalPanel();

		// adding it all.
		add(button_panel);
		add(sortby_panel);
		add(showing_records);
		add(table);
		add(record_navigation);
	}
}
